package com.bonuo.crm.service.impl;

import com.bonuo.crm.domain.Device;
import com.bonuo.crm.domain.Project;
import com.bonuo.crm.mapper.DeviceMapper;
import com.bonuo.crm.mapper.ProjectMapper;
import com.bonuo.crm.service.DeviceService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Override
    public List<Device> selectDeviceList(Device device) {
        //查询院系或代理商下的已购设备
        if(device.getFacultyId() != null || device.getAgentId() != null){
            Project project = new Project();
            //院系或代理商
            project.setAgentId(device.getAgentId());
            project.setFacultyId(device.getFacultyId());
            //查询该院系或代理商下的项目
            List<Project> list = projectMapper.selectProjectList(project);
            if(list.size() > 0){
                List<Integer> integerList = new ArrayList<>();
                for (Project project1 : list) {
                    integerList.add(project1.getId());
                }
                //通过项目id查询设备  赋值
                device.setProjectIds(integerList);
            }
        }
        if (device.getPageNum() != null && device.getPageSize() != null) {
            PageHelper.startPage(device.getPageNum(), device.getPageSize());
        }
        return deviceMapper.selectDeviceList(device);
    }

    @Override
    public Integer insertDevice(Device device) {
        return deviceMapper.insertDevice(device);
    }

    @Override
    public Integer deleteDeviceById(Integer id) {
        return deviceMapper.deleteDeviceById(id);
    }

    @Override
    public Integer updateDevice(Device device) {
        return deviceMapper.updateDevice(device);
    }

    @Override
    public Integer selectDeviceByProjectId(Integer id) {
        return deviceMapper.selectDeviceByProjectId(id);
    }

    @Override
    public BigDecimal selectAmount(Device device) {
        return deviceMapper.selectAmount(device);
    }

    @Override
    public BigDecimal selectBidAmount(Integer projectId) {
        return deviceMapper.selectBidAmount(projectId);
    }
}
